package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.app.oss.exception.ErrorCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.entity.AirplaneModel;
import com.sz.biz.logistics.base.service.AirplaneModelService;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.dto.FileInfoDto;
import com.sz.app.oss.api.logistics.base.dto.AirPlaneModelDto;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Function: AirPlaneModelController <br>
 * Author: Charles <br>
 * Date: 2016-12-02 10:19:00
 */
@RestController
@RequestMapping("${api}/biz/airplanemodels")
@Api(description = " ", tags = "026、机型管理")
public class AirPlaneModelController extends BaseController {
    @Autowired
    private AirplaneModelService airplaneModelService;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.biz.airplane.model}")
    private String i18nTableName;

    @ApiOperation(value = "添加机型", notes = "添加机型, <br>权限:" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @RequestMapping(value = "/", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    public ResultDto createAirPlaneModel(@RequestBody @ApiParam(value = "机型", required = true) AirPlaneModelDto model) {
        AirplaneModel entity = new AirplaneModel();
        BeanUtils.copyProperties(model, entity);
        entity.setOrdinal(0);
        Integer id = airplaneModelService.addAirplaneModel(entity, model.getPictures());
        i18nService.save(id, model.getI18n(), i18nTableName);
        ResultDto result = this.getSaveSuccessDto(MessagesCodes.MODULE_AIRPLANE_MODEL);
        return result;
    }

    @ApiOperation(value = "修改机型", notes = "修改机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "机型Id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "model", value = "机型", required = true,
                    dataType = "AirPlaneModelDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    public ResultDto updateAirPlaneModel(@PathVariable("id") int id, @RequestBody AirPlaneModelDto model) {
        AirplaneModel entity = airplaneModelService.getById(id);
        if (entity != null) {
            BeanUtils.copyProperties(model, entity);
            entity.setId(id);
            airplaneModelService.updateAirPlanModel(entity, model.getPictures());
        }
        i18nService.save(id, model.getI18n(), i18nTableName);
        ResultDto result = this.getUpdateSuccessDto(MessagesCodes.MODULE_AIRPLANE_MODEL);
        return result;
    }

    @ApiOperation(value = "删除机型", notes = "删除机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParam(name = "id", value = "机型Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    public ResultDto deleteAirPlaneModel(@PathVariable("id") int id) {
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        airplaneModelService.deleteByIds(ids);
        ResultDto result = this.getDeleteSuccessDto(MessagesCodes.MODULE_AIRPLANE_MODEL);
        return result;
    }

    @ApiOperation(value = "批量删除机型", notes = "批量删除机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    public ResultDto deleteAirPlaneModels(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        airplaneModelService.deleteByIds(ids);
        ResultDto result = this.getDeleteSuccessDto(MessagesCodes.MODULE_AIRPLANE_MODEL);
        return result;
    }

    @ApiOperation(value = "获取机型", notes = "获取机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParam(name = "id", value = "机型Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getAirPlaneModel(@PathVariable("id") int id) {
        AirPlaneModelDto dto = new AirPlaneModelDto();
        AirplaneModel entity = airplaneModelService.getById(id);
        if (entity != null) {
            BeanUtils.copyProperties(entity, dto);
            List<FileInfoDto> files = airplaneModelService.getAttachments(entity.getPicture());
            dto.setFiles(files);

            List<I18nDto> list = i18nService.findI18nDtoByRefId(i18nTableName, entity.getId());
            dto.setI18n(list);
        }
        ResultDto result = new ResultDto(dto);
        return result;
    }

    @ApiOperation(value = "检查机型是否存在", notes = "检查机型是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iata", value = "IATA编码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    public ResultDto checkIATAExist(@RequestParam(required = false) String iata,
                                    @RequestParam(required = false) Integer id) {
        boolean exist = false;
        if (iata != null && iata.length() > 0) {
            exist = id != null ? airplaneModelService.isIATAExist(iata, id) : airplaneModelService.isIATAExist(iata);
        }
        ResultDto result = this.convertBizException(exist
                ? Exceptions.bizException(ErrorCodes.ERROR_BIZ_AIRPLANEMODEL_IATA_EXIST, iata)
                : null);
        return result;
    }

    @ApiOperation(value = "获取机型", notes = "获取机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", required = false, dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public ResultDto getAirPlaneModels(@RequestParam(required = false) String q,
                                       @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                       @RequestParam(required = false, defaultValue = "20") int pageSize,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return airplaneModelService.getAirplaneModels(new QueryParams(pageSize, pageIndex, q, sort, asc));
    }

    @ApiOperation(value = "获取机型短接口", notes = "获取机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", required = false, dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/shortlist", method = {RequestMethod.GET})
    public ResultDto getShortAirPlaneModels(@RequestParam(required = false) String q,
                                       @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                       @RequestParam(required = false, defaultValue = "20") int pageSize,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return airplaneModelService.getShortAirplaneModels(new QueryParams(pageSize, pageIndex, q, sort, asc));
    }

    @ApiOperation(value = "获取机型(i18n)", notes = "获取机型<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言代码", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "q", value = "查询关键字", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "unfilled", value = "是否未添加国际化字段，true，代表未添加，fasle与null代表所有",
                    dataType = "boolean", paramType = "query", defaultValue = "false"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", required = false, dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/i18n/{language}", method = {RequestMethod.GET})
    public ResultDto getAirPlaneModelsWithI18n(@PathVariable String language,
                                               @RequestParam(required = false) String q,
                                               @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                               @RequestParam(required = false, defaultValue = "20") int pageSize,
                                               @RequestParam(required = false, defaultValue = "false") Boolean unfilled,
                                               @RequestParam(required = false) String sort,
                                               @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return airplaneModelService.getAirplaneModelsWithI18n(new QueryParams(pageSize, pageIndex, q, sort, asc, language)
                .put("unfilled", unfilled));
    }

    @ApiOperation(value = "保存机型国际化(i18n)", notes = "保存机型国际化信息<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    @RequestMapping(value = "/i18n", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_AIRPLANE)
    public ResultDto setAirPlaneModelI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true)
                                                  List<I18nDto> models) {
        i18nService.save(models, i18nTableName);
        airplaneModelService.refreshCache();
        ResultDto result = this.getSaveI18nSuccessDto();
        return result;
    }
}
